package ${t.packagePath}.controller;

import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.util.MapUtils;
import ${t.packagePath}.service.${t.roleTableName}Service;
import ${t.packagePath}.model.${t.roleTableName};
import ${t.packagePath}.utils.AjaxJson;
import ${t.packagePath}.config.StpSpUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;
import java.lang.reflect.Method;
import java.util.List;
import cn.hutool.core.util.ReflectUtil;
import javax.servlet.http.HttpServletRequest;
import io.swagger.annotations.ApiOperation;
import ${t.packagePath}.service.${t.mkNameBig}Service;
import ${t.packagePath}.model.${t.mkNameBig};
<#if cfg.saTokenAuthWay == 1 >import cn.dev33.satoken.stp.StpUtil;
<#else>import cn.dev33.satoken.annotation.SaCheckPermission;
</#if>


/**
 * Controller: 角色
 * @author ${cfg.author} 
 */
@RestController
@AllArgsConstructor(onConstructor_ = {@Autowired})
public class ${t.roleTableName}Controller {

	${t.roleTableName}Service ${t.roleTableName?uncap_first}Service;
	BeanSearcher searcher;

	/**
     * 增
     */
    @RequestMapping("/SpRole/add")
    @SaCheckPermission(type= Stp${t.userTableName}Util.TYPE,value=${t.roleTableName}.PERMISSION_CODE)
    @Transactional(rollbackFor = Exception.class)
    public AjaxJson add(${t.roleTableName} s) {
        List<${t.roleTableName}> list = ${t.roleTableName?uncap_first}Service.lambdaQuery().eq(${t.roleTableName}::getName, s.getName()).list();
			if (list.size() != 0) {
			return AjaxJson.getError("角色名称已被占用");
			}
		${t.roleTableName?uncap_first}Service.save(s);
		return AjaxJson.getSuccessData(s);
    }

	@ApiOperation("删")
	@${cfg.apiMappingWayString}("/SpRole/delete")
<#if cfg.saTokenAuthWay == 2 >    @SaCheckPermission(type= Stp${t.userTableName}Util.TYPE,value=${t.roleTableName}.PERMISSION_CODE)${cfg.line}</#if><#rt>
	public AjaxJson delete(${t.primaryKey.fieldType} id){
<#if cfg.saTokenAuthWay == 1 >        StpUtil.checkPermission(${t.modelName}.PERMISSION_CODE);${cfg.line}</#if><#rt>

		boolean b = ${t.roleTableName?uncap_first}Service.removeById(id);
		return AjaxJson.getByBoolean(b);
	}

	/**
    * 改
    */
    @RequestMapping("/SpRole/update")
    @SaCheckPermission(type= Stp${t.userTableName}Util.TYPE,value=${t.roleTableName}.PERMISSION_CODE)
    public AjaxJson update(${t.roleTableName} s) {
		boolean b = ${t.roleTableName?uncap_first}Service.updateById(s);
		return AjaxJson.getByBoolean(b);
    }

	@ApiOperation("查")
	@${cfg.apiMappingWayString}("/SpRole/getById")
<#if cfg.saTokenAuthWay == 2 >    @SaCheckPermission(type= Stp${t.userTableName}Util.TYPE,value=${t.roleTableName}.PERMISSION_CODE)${cfg.line}</#if><#rt>
		public AjaxJson getById(${t.primaryKey.fieldType} id){
		${t.roleTableName} ${t.roleTableName?uncap_first} = ${t.roleTableName?uncap_first}Service.getById(id);
		return AjaxJson.getSuccessData(${t.roleTableName?uncap_first});
	}

	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */
	@${cfg.apiMappingWayString}("/SpRole/getList")
<#if cfg.saTokenAuthWay == 2 >   @SaCheckPermission(type= Stp${t.userTableName}Util.TYPE,value=${t.roleTableName}.PERMISSION_CODE)${cfg.line}</#if><#rt>
	public AjaxJson getList(HttpServletRequest request) {
		SearchResult<${t.roleTableName}> search = searcher.search(${t.roleTableName}.class, MapUtils.flat(request.getParameterMap()));
    	return AjaxJson.getPageData(search.getTotalCount().longValue(), search.getDataList());
	}
}
